<?php

class FreeAction extends LoginAction {

    protected $_type = 'free';

    public function index() {
        $data = D('Free')->getIndexData();
        $this->assign('frees', $data['data']);
        $this->assign('page', $data['page']);
        $this->display();
    }

    public function add() {
        $this->getLoadData();
        if (isPost()) {
            $data = $this->getPostDate();
            $this->createAdd('Free', $data);
        }
        $this->display();
    }

    public function edit() {
        $id = intval(getGet('id'));
        if (!$id) {
            $this->error('信息获取必须提供数据标识！');
        }
        $this->getLoadData();
        //加载小样信息
        $free = M('Free')->find($id);
        if (isPost()) {
            $data = $this->getPostDate(true);
            $data['id'] = $id;
            $rs = $this->createUpdate('Free', $data, false);
            if ($rs) {
                $this->successMessage('修改成功！');
            }
            if ($data['image'] && $rs) {
                if ($data['image'] != $free['image']) {
                    $this->delUpLoadFile($free['image'], $this->_type);
                }
            } else if (!$rs && $data['image']) {
                $this->delUpLoadFile($data['image'], $this->_type);
                $this->error('更新失败！');
            }
            $free = M('Free')->find($id);
        }
        if ($free) {
            $this->assign('free', $free);
            //获取图片
            $imageUrl = $this->getManagedUrl($free['image'], $this->_type);
            $this->assign('imageUrl', $imageUrl);
        } else {
            $this->error('小样信息加载失败！');
        }
        $this->display();
    }

    /**
     * 获取提交数据
     * @param bool $isEdit 是否是修改
     * @return array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getPostDate($isEdit = false) {
        $data = array();
        $data['title'] = getPost('title');
        $data['content'] = getPost('content');
        $data['count'] = intval(getPost('count'));
        $data['start_time'] = strtotime(getPost('start_time'));
        $data['end_time'] = strtotime(getPost('end_time'));
        $data['receive_start_time'] = strtotime(getPost('receive_start_time'));
        $data['receive_end_time'] = strtotime(getPost('receive_end_time'));
        $data['mid'] = intval(getPost('market'));
        $data['fid'] = intval(getPost('floor'));
        $data['bid'] = intval(getPost('brand'));
        if (!$data['mid'] || !$data['fid'] || !$data['mid']) {
            $this->error('发放地点必须选择！');
        }
        if (!$data['count']) {
            $this->error('请填写正确的数量信息');
        }
        //小样活动时间
        if ($data['start_time'] > $data['end_time']) {
            $this->error('小样活动结束时间不能小于开始时间！');
        } else if ($data['end_time'] < strtotime(date('Y-m-d'))) {
            $this->error('小样结束时间不能小于当期时间！');
        }
        //小样领取时间
        if ($data['receive_start_time'] > $data['receive_end_time']) {
            $this->error('小样领取结束时间不能小于开始时间！');
        } else if ($data['receive_end_time'] < strtotime(date('Y-m-d'))) {
            $this->error('小样领取时间不能小于当期时间！');
        }
        //小样发放时间不能大于开始领取时间
        if ($data['receive_start_time'] > $data['start_time']) {
            $this->error('小样领取时间不大于小样发放时间！');
        }
        //文件执行上传并且保存返回媒体库标示
        $doUpload = true;
        if ($isEdit) {
            if (!$_FILES['image']['name']) {
                $doUpload = false;
            }
        }
        if ($doUpload) {
            $fId = $this->uploadFiles($this->_type);
            $data['image'] = $fId; //媒体库标示
        }
        return $data;
    }

    /**
     * 加载需要加载的数据
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getLoadData() {
        $market = M('Market')->field('m_id,m_name')->select();
        $this->assign('market', $market);
    }

}

?>
